﻿Imports BUS

Public Class frmTuVanHeThongDienNLMT

#Region "Thuoc tinh"
    Public status As Boolean = False
    Private _tongCongSuatTieuThuTrongNgay As Integer = 0
    Private _tongCongSuatPin As Integer = 0
    Private _dungLuongAcquy12V As Double = 0
    Private _dungLuongAcquy24V As Double = 0
    Private _congSuatBoKichDien As Integer = 0
    Private _dienApDauRaBoKichDien As Integer = 0
    Private _coThietBiDeBa As Boolean = False
    Private _tongCongSuatTieuThu1Gio = 0

#End Region

#Region "GetThuocTinh"
    Public Property CoThietBiDeBa() As Boolean
        Get
            Return _coThietBiDeBa
        End Get
        Set(ByVal value As Boolean)
            _coThietBiDeBa = value
        End Set
    End Property
    Public Property TongCongSuatTieuThu1Gio() As Object
        Get
            Return _tongCongSuatTieuThu1Gio
        End Get
        Set(ByVal value As Object)
            _tongCongSuatTieuThu1Gio = value
        End Set
    End Property
    Public Property TongCongSuatTieuThuTrongNgay() As Integer
        Get
            Return _tongCongSuatTieuThuTrongNgay
        End Get
        Set(ByVal value As Integer)
            _tongCongSuatTieuThuTrongNgay = value
        End Set
    End Property
    Public Property TongCongSuatPin() As Integer
        Get
            Return _tongCongSuatPin
        End Get
        Set(ByVal value As Integer)
            _tongCongSuatPin = value
        End Set
    End Property
    Public Property DungLuongAcquy12V() As Integer
        Get
            Return _dungLuongAcquy12V
        End Get
        Set(ByVal value As Integer)
            _dungLuongAcquy12V = value
        End Set
    End Property
    Public Property DungLuongAcquy24V() As Integer
        Get
            Return _dungLuongAcquy24V
        End Get
        Set(ByVal value As Integer)
            _dungLuongAcquy24V = value
        End Set
    End Property
    Public Property CongSuatBoKichDien() As Integer
        Get
            Return _congSuatBoKichDien
        End Get
        Set(ByVal value As Integer)
            _congSuatBoKichDien = value
        End Set
    End Property
    Public Property DienApDauRaBoKichDien() As Integer
        Get
            Return _dienApDauRaBoKichDien
        End Get
        Set(ByVal value As Integer)
            _dienApDauRaBoKichDien = value
        End Set
    End Property
#End Region

#Region "Load Default Setting"
    Private Sub LoadDefault()
        DefaultDuLieuThemVao()
        DefaultDiaPhuong()
        DefaultKetQuaTuVan()
        DefaultButton()
        btnClearAll.Enabled = False
        status = False
    End Sub

    Private Sub DefaultAttibutes()
        TongCongSuatTieuThuTrongNgay = 0
        TongCongSuatTieuThu1Gio = 0
        TongCongSuatPin = 0
        DungLuongAcquy12V = 0
        DungLuongAcquy24V = 0
        CongSuatBoKichDien = 0
        DienApDauRaBoKichDien = 0
    End Sub

    Private Sub DefaultButton()
        btnClearAll.Enabled = False
        btnQuayLai.Enabled = False

        btnTinhToan.Enabled = True

    End Sub
    Private Sub DefaultTanSuatCupDien()
        gbTanSuatCupDien.Enabled = False

        txtNgaySac.Text = "1"
        txtNgaySuDung.Text = "1"
    End Sub
    Private Sub DefaultDiaPhuong()
        gbThongTinDiaPhuong.Enabled = False
        cbDiaPhuong.SelectedIndex = 0
        txtSoGioNang.Text = cbDiaPhuong.SelectedValue.ToString()
    End Sub

    Private Sub DefaultDuLieuThemVao()
        gbDuLieuThemTuVan.Enabled = True
        cbNhuCauSuDung.SelectedIndex = 0
        dgvDuLieuThem.Rows.Clear()
        txtDienApDangSuDung.Text = "220"
    End Sub
    Private Sub DefaultKetQuaTuVan()
        gbKetQuaTuVan.Enabled = True
        lbCongSuatBoKichDien.Text = "0"
        lbTongCongSuatTieuThu.Text = "0"
        lbTongCongSuatPin.Text = "0"
        lbDungLuongAcQuy24.Text = "0"
        lbDienApDauRaBoKichDien.Text = "0"
        gbKetQuaTuVan.Enabled = False
    End Sub

#End Region

#Region "Load Indefault settings"

    Private Sub LoadInDefault()
        InDefaultDuLieuThemVao()
        InDefaultDiaPhuong()
        InDefaultTanSuatCupDien()
        InDefaultKetQuaTuVan()
        InDefaultButton()
        status = True
    End Sub

    Private Sub InDefaultButton()
        btnClearAll.Enabled = True
        btnQuayLai.Enabled = True

        btnTinhToan.Enabled = False

    End Sub

    Private Sub InDefaultTanSuatCupDien()
        gbTanSuatCupDien.Enabled = False
        'txtCoDien.Enabled = False
        'txtNgaySac.Enabled = False
        'txtNgaySuDung.Enabled = False
    End Sub

    Private Sub InDefaultDiaPhuong()
        gbThongTinDiaPhuong.Enabled = False
        'cbDiaPhuong.Enabled = False
        'txtSoGioNang.Text = cbDiaPhuong.SelectedValue.ToString()
        'txtSoGioNang.Enabled = False
    End Sub

    Private Sub InDefaultDuLieuThemVao()
        gbDuLieuThemTuVan.Enabled = False
        'cbNhuCauSuDung.SelectedIndex = 0
        'dgvDuLieuThem.Rows.Clear()
        'txtDienApDangSuDung.Text = "220"
    End Sub

    Private Sub InDefaultKetQuaTuVan()
        gbKetQuaTuVan.Enabled = True
        'lbCongSuatBoKichDien.Text = "0"
        'lbTongCongSuatTieuThu.Text = "0"
        'lbTongCongSuatPin.Text = "0"
        'lbDungLuongAcQuy24.Text = "0"
        'lbDienApDauRaBoKichDien.Text = "0"
        'gbKetQuaTuVan.Enabled = False
    End Sub
#End Region

#Region "Xu ly con"

    Private Sub LoadDiaPhuong()
        Dim dpt = DiaPhuongBUS.LoadDiaPhuong()
        cbDiaPhuong.DataSource = dpt
        cbDiaPhuong.DisplayMember = dpt.Columns("TenDiaPhuong").ToString()
        cbDiaPhuong.ValueMember = dpt.Columns("SoGioNang").ToString()
    End Sub

    Private Function KiemTraTinhToan() As Boolean
        If txtDienApDangSuDung.Text = "" Then
            MessageBox.Show("Chưa có dữ liệu Điện Áp đang sử dụng.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return False
        End If
        If cbNhuCauSuDung.SelectedIndex = 1 Then
            If txtNgaySac.Text = "" Or txtNgaySuDung.Text = "" Or txtSoGioNang.Text = "" Then
                MessageBox.Show("Chưa có thông tin Tần Suất Sạc.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Return False
            Else
                If txtNgaySac.Text = "0" Or txtNgaySuDung.Text = "0" Or txtSoGioNang.Text = "0" Then
                    MessageBox.Show("Số ngày Sạc và Số Giờ Nắng phải khác 0.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Return False
                End If
            End If
        End If
        If dgvDuLieuThem.Rows.Count <= 1 Then
            MessageBox.Show("Chưa có dữ liệu trên lưới.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return False
        End If
        Return True
    End Function

    Private Sub LoadKetQuaTuVan()
        lbTongCongSuatTieuThu.Text = TongCongSuatTieuThuTrongNgay.ToString()
        lbTongCongSuatTieuThu1Gio.Text = TongCongSuatTieuThu1Gio.ToString()
        lbTongCongSuatPin.Text = TongCongSuatPin.ToString()
        lbDungLuongAcQuy12.Text = DungLuongAcquy12V.ToString()
        lbDungLuongAcQuy24.Text = DungLuongAcquy24V.ToString()
        lbCongSuatBoKichDien.Text = CongSuatBoKichDien.ToString()
        lbDienApDauRaBoKichDien.Text = DienApDauRaBoKichDien.ToString()
    End Sub

    Private Function TrapKey(ByVal KCode As String) As Boolean
        If (KCode >= 48 And KCode <= 57) Or KCode = 8 Then
            TrapKey = False
        Else
            TrapKey = True
        End If
    End Function
#End Region

#Region "Xu ly controls"


    Private Sub btnXoaMot_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnXoaMot.Click
        If dgvDuLieuThem.RowCount > 1 Then
            If dgvDuLieuThem.SelectedRows.Count > 0 Then
                Dim a = dgvDuLieuThem.SelectedRows.Count - 1
                For Each i As DataGridViewRow In dgvDuLieuThem.SelectedRows
                    dgvDuLieuThem.Rows.Remove(i)
                Next
            End If
        End If
    End Sub

    Private Sub frmTuVanHeThongDienNLMT_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadDiaPhuong()
        LoadDefault()
    End Sub

    Private Sub btnXoaHet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoaHet.Click
        If dgvDuLieuThem.RowCount > 1 Then
            Dim result As DialogResult = MessageBox.Show("Bạn có chắc chắn xóa toàn bộ dữ liệu thêm vào trên lưới?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
            If result = Windows.Forms.DialogResult.Yes Then
                dgvDuLieuThem.Rows.Clear()
            End If
        End If
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        If status = True Then
            Dim result As DialogResult = MessageBox.Show("Thoát phiên làm việc hiện tại?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
            If result = Windows.Forms.DialogResult.Yes Then
                Me.Close()
            End If
        Else
            Me.Close()
        End If
    End Sub

    Private Sub btnClearAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearAll.Click
        LoadDefault()
    End Sub

    Private Sub cbDiaPhuong_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbDiaPhuong.SelectedIndexChanged
        txtSoGioNang.Text = cbDiaPhuong.SelectedValue.ToString()
    End Sub

    Private Sub btnTinhToan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTinhToan.Click
        If KiemTraTinhToan() = True Then
            btnTinhToan.Enabled = False
            TongCongSuatTieuThuTrongNgay = 0
            TongCongSuatTieuThu1Gio = 0
            For i = 0 To dgvDuLieuThem.Rows.Count - 2
                Try
                    TongCongSuatTieuThu1Gio += Integer.Parse(dgvDuLieuThem.Rows(i).Cells("CongSuatMotGio").Value.ToString()) + Integer.Parse(dgvDuLieuThem.Rows(i).Cells("CongSuatHaoHut").Value.ToString()) * Integer.Parse(dgvDuLieuThem.Rows(i).Cells("HeSoKhoiDong").Value.ToString()) * Integer.Parse(dgvDuLieuThem.Rows(i).Cells("SoLuong").Value.ToString())
                    TongCongSuatTieuThuTrongNgay += TongCongSuatTieuThu1Gio * Integer.Parse(dgvDuLieuThem.Rows(i).Cells("ThoiGianSuDung").Value.ToString())
                    If Integer.Parse(dgvDuLieuThem.Rows(i).Cells("HeSoKhoiDong").Value.ToString()) > 1 Then
                        CoThietBiDeBa = True
                    End If
                Catch ex As Exception
                    MessageBox.Show("Dữ liệu trên lưới không đúng dạng số.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Return
                End Try
            Next
            CongSuatBoKichDien = TongCongSuatTieuThuTrongNgay
            DienApDauRaBoKichDien = Integer.Parse(txtDienApDangSuDung.Text)
            If cbNhuCauSuDung.SelectedIndex = 0 Then
                TongCongSuatPin = TongCongSuatTieuThuTrongNgay
            Else

                TongCongSuatPin = TongCongSuatTieuThuTrongNgay * Integer.Parse(txtNgaySuDung.Text) / (txtNgaySac.Text * txtSoGioNang.Text)
                DungLuongAcquy12V = 1.0 * TongCongSuatPin / 2
                DungLuongAcquy24V = DungLuongAcquy12V / 2


            End If
            LoadInDefault()
        End If

        LoadKetQuaTuVan()

    End Sub

    Private Sub cbNhuCauSuDung_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbNhuCauSuDung.SelectedIndexChanged
        If cbNhuCauSuDung.SelectedIndex = 0 Then
            gbTanSuatCupDien.Enabled = False
            gbThongTinDiaPhuong.Enabled = False
        ElseIf cbNhuCauSuDung.SelectedIndex = 1 Then
            gbTanSuatCupDien.Enabled = True
            gbThongTinDiaPhuong.Enabled = True
        End If
    End Sub

    Private Sub btnQuayLai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuayLai.Click

        gbDuLieuThemTuVan.Enabled = True
        cbNhuCauSuDung_SelectedIndexChanged(sender, e)
        DefaultButton()
        gbKetQuaTuVan.Enabled = True
        DefaultAttibutes()

    End Sub


    Private Sub btnThemDiaPhuong_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemDiaPhuong.Click
        Dim result As DialogResult = frmDiaPhuong.ShowDialog()
        If result = Windows.Forms.DialogResult.OK Then
            LoadDiaPhuong()
        End If
    End Sub

    Private Sub txtDienApDangSuDung_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDienApDangSuDung.KeyPress
        e.Handled = TrapKey(Asc(e.KeyChar))
    End Sub

    Private Sub txtNgaySac_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNgaySac.KeyPress
        e.Handled = TrapKey(Asc(e.KeyChar))
    End Sub

    Private Sub txtNgaySuDung_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNgaySuDung.KeyPress
        e.Handled = TrapKey(Asc(e.KeyChar))
    End Sub

    Private Sub txtSoGioNang_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSoGioNang.KeyPress
        e.Handled = TrapKey(Asc(e.KeyChar))
    End Sub

    Private Sub btnXemDanhSachHeThongPhuHop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXemDanhSachHeThongPhuHop.Click
        Dim fhethongphuhop As New frmHeThongPhuHop
        fhethongphuhop.NhanGiaTri(cbNhuCauSuDung.SelectedIndex, TongCongSuatTieuThuTrongNgay, TongCongSuatPin, TongCongSuatTieuThu1Gio, DungLuongAcquy12V, DungLuongAcquy24V, CongSuatBoKichDien, DienApDauRaBoKichDien, CoThietBiDeBa, Integer.Parse(txtNgaySac.Text), Integer.Parse(txtNgaySuDung.Text))
        fhethongphuhop.Show()
    End Sub
#End Region

    
End Class
